package com.wjl.picture.picture.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaCheckRole;
import com.wjl.picture.auth.constant.SpaceUserPermissionConstant;
import com.wjl.picture.common.base.BaseResponse;
import com.wjl.picture.common.base.ResultUtils;
import com.wjl.picture.common.exception.ErrorCode;
import com.wjl.picture.common.exception.ThrowUtils;
import com.wjl.picture.picture.model.request.PictureUploadByBatchRequest;
import com.wjl.picture.picture.model.request.PictureUploadRequest;
import com.wjl.picture.picture.model.vo.PictureVO;
import com.wjl.picture.picture.service.PictureService;
import com.wjl.picture.user.model.constant.UserConstant;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

@RestController("/picture/file")
public class FileController {
    @Resource
    private PictureService pictureService;
    /**
     * 上传图片（可重新上传）
     */
    @PostMapping("/upload")
    @SaCheckPermission(SpaceUserPermissionConstant.PICTURE_UPLOAD)
    public BaseResponse<PictureVO> uploadPicture(@RequestPart("file") MultipartFile multipartFile, PictureUploadRequest pictureUploadRequest) {
        PictureVO pictureVO = pictureService.uploadPicture(multipartFile, pictureUploadRequest);
        return ResultUtils.success(pictureVO);
    }
    /**
     * 上传图片（可重新上传）
     */
    @PostMapping("/upload/url")
    @SaCheckPermission(SpaceUserPermissionConstant.PICTURE_UPLOAD)
    public BaseResponse<PictureVO> uploadPictureByUrl(PictureUploadRequest pictureUploadRequest) {
        PictureVO pictureVO = pictureService.uploadPicture(pictureUploadRequest.getFileUrl(), pictureUploadRequest);
        return ResultUtils.success(pictureVO);
    }

    /**
     * 批量上传图片
     * @param pictureUploadByBatchRequest
     * @return
     */
    @PostMapping("/upload/batch")
    @SaCheckRole(value = UserConstant.USER_ROLE_ADMIN)
    public BaseResponse<Integer> uploadPictureByBatch(@RequestBody PictureUploadByBatchRequest pictureUploadByBatchRequest) {
        ThrowUtils.throwIf(pictureUploadByBatchRequest == null, ErrorCode.PARAMS_ERROR);
        int uploadCount = pictureService.uploadPictureByBatch(pictureUploadByBatchRequest);
        return ResultUtils.success(uploadCount);
    }


}
